package com.c.ctgproject.adapter.guang

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseEmptyRecyclerViewAdapterV2
import com.c.ctgproject.entity.guang.BatteryHandlerEntityV3
import com.c.ctgproject.ui.collect.HandlerBatteryDetailActivity
import com.c.ctgproject.utils.MyConstants
import com.c.ctgproject.utils.noDataHandler
import org.jetbrains.anko.startActivity

class BatteryHandlerAdapter(private val context: Context, private val mList: List<BatteryHandlerEntityV3.DataBean.ListBean>, private val listener: BatteryHandlerAdapterListener) : BaseEmptyRecyclerViewAdapterV2(context, mList) {
    override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_battery_handler_list, parent, false)
        return MyViewHolder(view)
    }

    override fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            val data = mList[position]
            "创建人:${data.createPersonName.noDataHandler()}".also { holder.mCreateNameTV.text = it }
            holder.mCreateTimeTV.text = data.createTime.noDataHandler()
            holder.mPlanTimeTV.text = data.plainFinishTime.noDataHandler()
            holder.mHandlerNo.text = data.pbDismantleNo.noDataHandler()
            val batteryType = when (data.batteryType) {
                1 -> "电池包"
                2 -> "模组"
                3 -> "电芯"
                else -> "暂无数据"
            }
            holder.mBatteryModelTV.text = batteryType
            val enterMode = when (data.enterMode) {
                1 -> "整车电池"
                2 -> "散件"
                3 -> "电池拆解"
                else -> "暂无数据"
            }
            holder.mInType.text = enterMode


//            holder.mTimeTV.text = data.realFinishTime.noDataHandler()
            //0-待入库 1-已入库 2-已拆解 3-已出库 4-拆解中
//            val statue = when (data.status) {

            when (data.dismantle) {
                4 -> {
//                    holder.mStatueTV.textColorResource = R.color.colorAccent
                    holder.mLine.visibility = View.VISIBLE
                    holder.mFinishRL.visibility = View.VISIBLE
                    "拆解中"
                }
                2 -> {
//                    holder.mStatueTV.textColorResource = R.color.red
                    holder.mLine.visibility = View.GONE
                    holder.mFinishRL.visibility = View.GONE
                    "拆解完成"
                }

                else -> {
                    holder.mLine.visibility = View.VISIBLE
                    holder.mFinishRL.visibility = View.VISIBLE
                    ""
                }
            }
//            holder.mStatueTV.text = statue

            holder.mFinishRL.setOnClickListener {
                holder.mFinishBTN.performClick()
            }
            holder.mFinishBTN.setOnClickListener {
                listener.onItemBtnClick(position)
            }
            holder.mBatteryCodeTV.text = data.batterySelfCode.noDataHandler()
            holder.mBatteryNameTV.text = data.batteryName.noDataHandler()

//            holder.mCarCodeTV.text=data.carCode.noDataHandler()
//            val batterySource =when(data.batterySource){
//                1->"拆解"
//                2->"散件"
//                else->"暂无数据"
//            }
//            holder.mBatteryFromTV.text=batterySource
            //详情
            holder.mParentLL.setOnClickListener {
                context.startActivity<HandlerBatteryDetailActivity>(
                    MyConstants.PB_ID to data.pbId
                )
            }
            //holder.mDeleteBTN.visibility=if(data.isRemove==1)View.VISIBLE else View.GONE
            holder.mDeleteBTN.visibility=View.GONE
            holder.mDeleteBTN.setOnClickListener {
                listener.deleteItem(position,data)
            }
        }
    }
    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val mHandlerNo: TextView = view.findViewById(R.id.mHandlerNo)
        val mBatteryModelTV: TextView = view.findViewById(R.id.mBatteryModelTV)
        val mCreateNameTV: TextView = view.findViewById(R.id.mCreateNameTV)
        val mInType: TextView = view.findViewById(R.id.mInType)
        val mCreateTimeTV: TextView = view.findViewById(R.id.mCreateTimeTV)

        //        val mStatueTV: TextView = view.findViewById(R.id.mStatueTV)
        val mPlanTimeTV: TextView = view.findViewById(R.id.mPlanTimeTV)

        //        val mTimeTV: TextView = view.findViewById(R.id.mTimeTV)
        val mFinishBTN: Button = view.findViewById(R.id.mFinishBTN)
        val mFinishRL: RelativeLayout = view.findViewById(R.id.mFinishRL)
        val mLine: View = view.findViewById(R.id.mLine)
        val mBatteryCodeTV: TextView = view.findViewById(R.id.mBatteryCodeTV)
        val mBatteryNameTV: TextView = view.findViewById(R.id.mBatteryNameTV)
        val mParentLL: LinearLayoutCompat = view.findViewById(R.id.mParentLL)

        //        val mBatteryFromTV: TextView = view.findViewById(R.id.mBatteryFromTV)
//        val mCarCodeTV: TextView = view.findViewById(R.id.mCarCodeTV)
//        val mFinishLL: LinearLayoutCompat = view.findViewById(R.id.mFinishLL)
        val mDeleteBTN: Button = view.findViewById(R.id.mDeleteBTN)
    }

    interface BatteryHandlerAdapterListener {
        fun onItemBtnClick(position: Int)
        fun deleteItem(position:Int,data:BatteryHandlerEntityV3.DataBean.ListBean)
    }
}